package org.kukker;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.telephony.Phone.State;
import android.util.Log;
import android.telephony.CallStateException;
import android.telephony.CallerInfo;
import android.telephony.Phone;
import android.telephony.PhoneBase;
import android.telephony.PhoneStateIntentReceiver;

public class CallIntentHandler extends Handler{ 

	private static final int PHONECALLSTATE_RECOGNIZE_ID = 1;
	private static final int PHONESERVICESTATE_RECOGNIZE_ID = 2;
	private static final int PHONEMESSAGEWAITINGSTATE_RECOGNIZE_ID = 3;
	
	public Context context;
	private PhoneStateIntentReceiver psir;
	private Thread thread;
	private CallerInfo callerInfo;
	
	public CallIntentHandler(PhoneStateIntentReceiver psir){
		this.psir = psir;
		psir.notifyPhoneCallState(PHONECALLSTATE_RECOGNIZE_ID);
		psir.notifyServiceState(PHONESERVICESTATE_RECOGNIZE_ID);
		psir.notifyMessageWaitingIndicator(PHONEMESSAGEWAITINGSTATE_RECOGNIZE_ID);
	}

	
	public void handleMessage(Message msg) {
	
		/*
		
		if(thread==null){
			thread = new Thread(
				new Runnable(){
					String lastState="";
					public void run() {
						while(true){
							CallerInfo callInfo = new android.telephony.CallerInfo();
							String callerNumber = callInfo.phoneNumber;
						
							Log.i("Kukker","caller:"+callerNumber);
							
							try {
								
								Phone phone = android.telephony.PhoneFactory.getDefaultPhone();
								List<Phone> phones = android.telephony.PhoneFactory.getAllPhones();
								int numPhones = phones.size();
								Log.i("Kukker","numPhones:"+numPhones);
								if (phone != null){
									Log.i("Kukker","defaultPhone is not null");
									phone.rejectCall();
								}else{
									Log.i("Kukker","defaultPhone is null");
								}
							} catch (CallStateException e1) {
								Log.i("Kukker",e1.getMessage());
								e1.printStackTrace();
							} catch( Exception e1){
								Log.i("Kukker",e1.getMessage());
								e1.printStackTrace();
							}
							
							android.telephony.Phone.State phoneState = (android.telephony.Phone.State) psir.getPhoneState();
							String currentState = phoneState.toString();
							if(currentState.compareTo(lastState)==0){
								lastState = currentState;
								
								Log.i("Kukker",phoneState.toString());
								psir.clearAbortBroadcast();							
								//psir.setResultCode(Activity.RESULT_OK);
							}
							
							
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}		
				}
			);
			
			thread.start();
		}
		*/
		
		Log.i("Kukker","handleMessage, "+msg.toString());
		if (msg.what == PHONECALLSTATE_RECOGNIZE_ID) {            
			State phoneState = psir.getPhoneState();
            switch (phoneState) {
            
            case OFFHOOK:
                    Log.i("Kukker", "****Phone picked up!");
                    break;
                    
            case RINGING:
                    Log.i("Kukker", "****Phone ringing!");
                    
                    break;
                    
            case IDLE:
                    Log.i("Kukker", "****Phone idle!");
                    break;
                   
            default:
            	 	Log.i("Kukker", "****Phone danger!");
                    break;
            }
		}
		if (msg.what == PHONESERVICESTATE_RECOGNIZE_ID) {            
			State serviceState = psir.getPhoneState();
            switch (serviceState) {
            
            case OFFHOOK:
                    Log.i("Kukker", "****Service picked up!");
                    break;
                    
            case RINGING:
                    Log.i("Kukker", "****Service ringing!");
                    // handleRing();
                    break;
                    
            case IDLE:
                    Log.i("Kukker", "****Service idle!");
                    break;
                   
            default:
            	 	Log.i("Kukker", "****Service danger!");
                    break;
            }
		}
	}

}